package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.UserDao;
import com.delivery.www.po.User;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    //用户登录
    @Override
    public User login(String name, String password) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql = "select * from user where user_name=? and password=?";
        Object[] params = {name, password};
        User user = null;
        rs = JdbcUtils.excute(connection, pstm, rs, sql, params);
        try {
            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setPassword(rs.getString("password"));
                user.setBalance(rs.getFloat("balance"));
                user.setUserEmail(rs.getString("user_email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.closeResource(connection, pstm, rs);
        return user;
    }

    @Override
    public int register(String name, String password, Float balance, String email) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        String sql = "insert into user (user_name,password,balance,user_email) values (?,?,?,?)";
        Object[] params = {name, password, balance, email};
        int i = JdbcUtils.excute(connection, pstm, sql, params);
        JdbcUtils.closeResource(connection, pstm, null);
        return i;
    }

    //充值
    @Override
    public int recharge(int userId, Float value) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {value, userId};
        String sql = "update user set balance = balance+? where user_id = ?";
        int i = JdbcUtils.excute(connection, pstm, sql, params);
        JdbcUtils.closeResource(connection, pstm, null);
        return i;
    }

    //获取所有用户
    @Override
    public List<User> getUser() {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<>();
        Object[] params = {};
        String sql = "select u.user_name from user u";
        rs = JdbcUtils.excute(connection, pstm, rs, sql, params);
        try {
            while (rs.next()) {
                User user = new User();
                user.setUserName(rs.getString("user_name"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.closeResource(connection, pstm, rs);
        return users;
    }
}
